package tv.acgn.advertisement.publish.service.impl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import tv.acgn.advertisement.publish.dao.AdUserDao;
import tv.acgn.advertisement.publish.dao.CreativeDao;
import tv.acgn.advertisement.publish.dao.dsl.CreativeDslDao;
import tv.acgn.advertisement.publish.enumconstant.AuditStatus;
import tv.acgn.advertisement.publish.exception.AbstractAdPubException;
import tv.acgn.advertisement.publish.exception.CreativeNotFoundException;
import tv.acgn.advertisement.publish.exception.CreativeSameNameHasExistException;
import tv.acgn.advertisement.publish.exception.UserNotFoundExceptionAbstract;
import tv.acgn.advertisement.publish.po.CreativePO;
import tv.acgn.advertisement.publish.service.CreativeService;
import tv.acgn.advertisement.publish.vo.creative.CreativeRequestVO;
import tv.acgn.advertisement.publish.vo.creative.CreativeResponseVO;

/**
 * @author : junG
 * @program : advertisement-ad-pub-service-impl
 * @description : 创意 业务实现类
 * @date : 2019-05-27 00:25
 **/

@Service
public class CreativeServiceImpl implements CreativeService {

    @Autowired
    private CreativeDao creativeDao;

    @Autowired
    private CreativeDslDao creativeDslDao;

    @Autowired
    private AdUserDao adUserDao;

    @Override
    public CreativeResponseVO addCreative(CreativeRequestVO requestVO) throws AbstractAdPubException {

        adUserDao.findById(requestVO.getUserId())
            .orElseThrow(UserNotFoundExceptionAbstract::new);

        CreativePO creativePO = new CreativePO();
        creativePO.setUserId(requestVO.getUserId())
            .setName(requestVO.getName())
            .setType(requestVO.getType())
            .setMaterialType(requestVO.getMaterialType())
            .setHeight(requestVO.getHeight()).setWidth(requestVO.getWidth())
            .setSize(requestVO.getSize())
            .setDuration(requestVO.getDuration())
            .setURI(requestVO.getURI())
            .setAuditStatus(AuditStatus.UN_CHECKED.getType())
            .setCreateTime(LocalDateTime.now())
            .setUpdateTime(LocalDateTime.now());

        if (!creativeDao.findOne(Example.of(creativePO)).isEmpty()) {
            throw new CreativeSameNameHasExistException();
        }

        creativeDao.save(creativePO);

        return new CreativeResponseVO()
            .setCreativeId(creativePO.getCreativeId())
            .setCreativeName(creativePO.getName());
    }

    @Override
    public CreativePO getCreative(String creativeId) throws AbstractAdPubException {
        CreativePO creativePO = creativeDao.findById(creativeId).orElseThrow(CreativeNotFoundException::new);
        return creativePO;
    }

    @Override
    public CreativeResponseVO deleteCreative(String creativeId) throws AbstractAdPubException {
        CreativePO creativePO = creativeDao.findById(creativeId).orElseThrow(CreativeNotFoundException::new);
        creativeDao.delete(creativePO);
        return new CreativeResponseVO()
            .setCreativeId(creativePO.getCreativeId())
            .setCreativeName(creativePO.getName());
    }

    @Override
    public CreativeResponseVO updateCreative(CreativeRequestVO requestVO) throws AbstractAdPubException {

        CreativePO creativePO = creativeDao.findById(requestVO.getCreativeId()).orElseThrow(
            CreativeNotFoundException::new);
        String ownerId = creativePO.getUserId();
        //TODO 判断请求上下文中UserId 是否等于 ownerId 不相等抛出非用户文件异常
        /*
         * 设置审核状态为空 不能使用此方法更新审核状态
         * 要使用 带有权限校验的 专有方法更新审核状态
         */
        requestVO.setAuditStatus(null);
        creativeDslDao.update(creativePO, requestVO);

        return new CreativeResponseVO()
            .setCreativeId(creativePO.getCreativeId())
            .setCreativeName(creativePO.getName());
    }
}
